package com.neuedu.autosale.cli.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.autosale.cli.entity.Sale;
import com.neuedu.autosale.cli.service.SaleService;
import com.neuedu.autosale.entity.Pager;
import com.neuedu.autosale.entity.Result;

@Controller
public class SaleController {
	@Autowired
	private SaleService saleService;
	
	@RequestMapping("sale/index")
	private String saleIndex() {
		return "client/salelist";
	}
	
	@RequestMapping("sale/findall")
	@ResponseBody
	public List<Sale> findAll(){
		return saleService.findAll();
	}
	
	@RequestMapping("sale/list")
	@ResponseBody
	public Pager<Sale> saleList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows){
		Pager<Sale> pager=new Pager<>(page,rows);
		List<Sale> saleList=saleService.findListByPager(pager);
		int total =saleService.findTotalByPager(pager);
		pager.setRows(saleList);
		pager.setTotal(total);
		return pager;
	}
	
	@RequestMapping("sale/add")
	@ResponseBody
	public Result doAdd(Sale sale) {
		Result result=saleService.add(sale);
		return result;
	}
	
	@RequestMapping("sale/edit")
	@ResponseBody
	public Result doEdit(Sale sale) {
		Result result=saleService.edit(sale);
		return result;
	}
	
	@RequestMapping("sale/delete")
	@ResponseBody
	public Result doDelete(Integer saleId) {
		List<Sale> list=new ArrayList<Sale>();
		list.add(new Sale(saleId));
		Result result=saleService.deleteByIds(list);
		return result;
	}
	

}
